﻿(function ($) {


    var _centreTarget;
    var _northTarget;
    var _sountTarget;
    var _eastTarget;
    var _westTarget;



    var IsDefault = false;
    var Target;
    var running = false;

    var contentType = "";

    jQuery.extend({ //SIM: Just create random number. Add random number end of the url.
        CreateAjaxUrl: function (url) {
            if (url) {
                var start = url.indexOf("r=");
                if (start >= 0) {
                    var url_new = url.substring(0, start - 1);

                    var end = url.indexOf("&", start + 1);
                    if (end > start) {
                        if (url.length > end) {
                            url_new += url.substring(end, url.length - 1);
                        }
                    }
                    url = url_new;
                }
                if (url.indexOf("?") >= 0) {
                    url += "&";
                }
                else {
                    url += "?";
                }
                url += "r=" + Math.random();
            }

            return url;
        }
    });

    jQuery.extend({
        IsIE: function () {
            return jQuery.browser.msie == true;
        }
    });

    jQuery.extend({
        IsIE7: function () {
            if (jQuery.IsIE) {
                return jQuery.browser.version == "7.0";
            }
            return false;
        }
    });

    jQuery.extend({
        IsIE8: function () {
            if (jQuery.IsIE) {
                return jQuery.browser.version == "8.0";
            }
            return false;
        }
    });

    jQuery.extend({
        RaiseHashChange: function () {
            //$(window).trigger("hashchange");
        }
    });


    jQuery.extend({
        Url: function (url) {
            if (url === undefined) {
                return top.location;
            }
            top.location.href = url;
        }
    });

    jQuery.extend({
        Hash: function (url) {
            if (url === undefined) {
                var hash = top.location.hash;
                if (hash) {
                    return hash.substring(1);
                }
                return "";
            }
            top.location.hash = url;
        }
    });

    jQuery.extend({
        HasBody: function (data) {
            return data.indexOf("<body") > 0;
        }
    });

    jQuery.extend({
        HasPopup: function (data) {
            return data.indexOf("class=\"popup-container\"") > 0;
        }
    });

    jQuery.extend({
        Reload: function () {
            top.location.reload();
        }
    });


    jQuery.extend({
        ShowLoading: function () {
          // $("body").css("overflow", "hidden");
            $("#AjaxLoading").dialog(
            {
               
                modal: true,
                position: "center",
                autoOpen: true,
                stack: false,
                resizable: false,
                draggable: false,
                height: "auto",
                width: "auto",
                minHeight: 0
                
            });
        }
    });

    jQuery.extend({
        HideLoading: function () {
          //  $("body").css("overflow", "auto");
            $("#AjaxLoading").dialog("close");
        }
    });
    jQuery.extend({
        HidePopupAndReload: function () {
            jQuery.HidePopup();
            var hash = jQuery.Hash();
            if (hash) {
                jQuery.ShowLoading();
                jQuery.LoadAjax(hash);
            }
        }
    });

    jQuery.extend({
        ShowInformationAndReload: function (message) {
            jQuery.HidePopup();
            //jQuery.Information(null, message, function () {                
            var hash = jQuery.Hash();
            if (hash) {
                jQuery.ShowLoading();
                jQuery.LoadAjax(hash);
            }
            //});            
        }
    });


    jQuery.extend({
        AjaxResponse: function (XMLHttpRequest) {
            this.ContentType = (XMLHttpRequest.getResponseHeader("content-type") || "").toLowerCase();
            this.IsContentHtml = this.ContentType.indexOf("html") >= 0;
            this.IsContentJS = this.ContentType.indexOf("javascript") >= 0;
            this.IsContentJSON = this.ContentType.indexOf("json") >= 0;
            this.Content = XMLHttpRequest.responseText;
        }
    });

    jQuery.extend({ 
        OnSuccess: function (data, textStatus, XMLHttpRequest) {
            var ajaxResponse = new jQuery.AjaxResponse(XMLHttpRequest);

            if (ajaxResponse.IsContentHtml) {
                if (jQuery.HasBody(data)) {
                    var hash = jQuery.Hash();
                    if (hash && hash != "") {
                        jQuery.Url(hash);
                    }
                }
                else if (jQuery.HasPopup(data)) {
                    jQuery.ShowPopup(ajaxResponse);
                }
                else {
                    $(Target).html(data); 
                }
            }
        }
    });

    jQuery.extend({
        ajaxSend: function () {
            running = true;
            jQuery.ShowLoading();
        }
    });

    jQuery.extend({
        ajaxComplete: function () {
            running = false;
            jQuery.HideLoading();
        }
    });


    jQuery.extend({
        LoadAjax: function (url) {
            if ($("div#loginarea").length == 0) { //SIM: If it's not login page
                if (url) {
                    url = jQuery.CreateAjaxUrl(url);
                    $.ajax({
                        url: url,
                        success: jQuery.OnSuccess, //SIM: If status is 200 then success
                        complete: jQuery.ajaxComplete
                    });
                }
            }
        }
    });


    $(window).ajaxSend(jQuery.ajaxSend);
    $(window).ajaxComplete(function () { try { jQuery.ajaxComplete(); } catch (ex) { location.href = "login" } });


    jQuery.extend({
        PageLoad: function (hash) {
            // hash doesn't contain the first # character.
            if (hash) { //SIM :If it contains #
                // restore ajax loaded state                                
                IsDefault = true;
                jQuery.LoadAjax(hash);
            } else {
                // start page
                if ($("div#loginarea").length == 0) {
                    jQuery.LoadAjax(startPage);
                }
                else {
                    if (IsDefault) {
                        jQuery.Reload();
                    }
                }
            }
        }
    });

    var startPage = "/Home/Start";

    $.fn.AjaxHistory = function (homePage) {
        Target = this; //SIM: This Target actually "#PageContainer" .
        if (homePage) {
            startPage = homePage;
        }
        return this;
    };


})(jQuery);




var i = 1;
function addTab() {
    $('#tabs').tabs("add", "#tabs-" + i, "tab" + i);   
    $("#tabs-" + i).html("#tabs-" + i);
    $('#tabs').tabs('select', i); // 
    i++;


}


function removeTab() {
    if (i >= 1) {
        $('#tabs').tabs("remove", i)

        i--;

    }

}

function tabCount() {

    var count = $('#tabs').tabs('length');
    alert(count);

    return;
}

function renameTAb(index, Newlabel) {

    if (index == nul) {
        index = 0;
    }

    $('#tabs').tabs('label', index, Newlabel);

}

//return tab index by tabCaption
function GetTabIndex(tabCaption) {
    var tabIndex = -1;

    $('#tabs ul li').each(
     function (index) {

         var caption = $(this).find('a').find('span').html();
         if ($.trim(caption) == tabCaption) {
             tabIndex = index;
             return true;

         }

     }
     );

    return tabIndex;
}

